<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop">
          <template>
              <div style="width:100%">
                  <div style="float:left;width:100%">
                      <table style="font-size:12px" class="table1style">
                          <tr>
                              <td>报告单元</td>
                              <td>
                                  <rm-ddl-rptunit prgno="LIS323" v-model="txtrptunit" filterable :clearable="false" defaultflag="1" @change="handlerrptitemchange($event)" :appendtobody="true" visible-change:true width="100%"></rm-ddl-rptunit>
                              </td>
                              <td>标本日期</td>
                              <td>
                                  <el-date-picker v-model="sampledatetime" type="date" placeholder="选择日期" width="100%" value-format="yyyy-MM-dd">
                                  </el-date-picker>
                              </td>
                              <td>
                                  <el-checkbox true-label="1" false-label="0" v-model="sameorgreqno">仅原始申请单号相同的</el-checkbox>
                              </td>
                              <td>
                                  <el-checkbox true-label="1" false-label="0" v-model="showalltargetitem">显示所有包含目标项目的标本</el-checkbox>
                              </td>
                          </tr>
                          <tr>
                              <td>输入模版</td>
                              <td align="left">
                                  <el-select v-model="bindmdl" filterable clearable style="width: 100%">
                                      <el-option v-for="(item,index) in inputMdlOptions" :key="index" :value="item.bindmdlid" :label="item.mdlname"></el-option>
                                  </el-select>
                              </td>
                              <td>病历号</td>
                              <td align="left">
                                  <el-input v-model="patno" style="width:100%;"></el-input>
                              </td>
                              <td>
                                  <el-checkbox true-label="1" false-label="0" v-model="showbinded" >显示已合并标本</el-checkbox>
                              </td>
                          </tr>
                          <tr>
                              <td style="font-weight:boleder;font-size:12px">样本号</td>
                              <td colspan="3" class="samplenoclass">
                                  <el-input v-model="sampleno" style="width:100%;"></el-input>
                              </td>
                              <td>
                                  <el-checkbox true-label="1" false-label="0" v-model="override">结果存在即覆盖</el-checkbox>
                              </td>
                          </tr>
                      </table>
                  </div>
                  <div style="float:left;width:100%;">
                      <table style="float:right">
                          <tr>
                              <td style="text-align:right">
                                  <el-button icon="el-icon-search" @click="Search()">查询</el-button>
                                  <el-button icon="el-icon-document" @click="canmerge()">合并</el-button>
                              </td>
                          </tr>
                      </table>
                  </div>
              </div>
          </template>
      </div>
      <div class="pagecenter">
          <div class="middle-center">
              <vxe-table :loading="loadingRight" :data.sync="tableDataLeft" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row ref="xTable1" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe @current-change="currentChangeEvent" @cell-click="currentChangeEvent">
                  <vxe-table-column field="sampledate" title="标本日期" width="120px" align="center"></vxe-table-column>
                  <vxe-table-column field="sampleno" title="标本编号" width="80px" align="center"></vxe-table-column>
                  <vxe-table-column field="pat_name" title="病人姓名" width="80px" align="center"></vxe-table-column>
                  <vxe-table-column field="pat_no" title="病人号" width="120px"></vxe-table-column>
                  <vxe-table-column field="pat_sex" title="性别" width="40px" align="center" :formatter="['formatSelect', sexOption]"></vxe-table-column>
                  <vxe-table-column field="pat_agestr" title="年龄" width="40px" align="center"></vxe-table-column>
                  <vxe-table-column field="rpt_itemname" title="检验项目" width="120px"></vxe-table-column>
                  <vxe-table-column field="result_str" title="检验结果" width="80px" align="right" header-align="center"></vxe-table-column>
                  <vxe-table-column field="specimen_name" title="标本类型" width="80px" align="center"></vxe-table-column>
                  <vxe-table-column field="deptname" title="申请科室" width="120px"></vxe-table-column>
                  <vxe-table-column field="req_bedno" title="床号" width="80px"></vxe-table-column>
                  <vxe-table-column field="barcode" title="条码号" width="120px"></vxe-table-column>
                  <vxe-table-column field="rptunitname" title="报告单元ID" width="100px"></vxe-table-column>
                  <vxe-table-column field="rechk_dt" title="审核时间" width="100px"></vxe-table-column>
                  <vxe-table-column field="rechkuser" title="审核医生" width="80px" align="center"></vxe-table-column>
                  <vxe-table-column field="codena" title="病人类别" width="100px" align="center"></vxe-table-column>
                  <vxe-table-column field="wardname" title="病人病区" width="100px"></vxe-table-column>
                  <vxe-table-column field="emer_flag" title="加急" width="40px" align="center">
                      <template v-slot="{ row }">
                          <el-checkbox false-label="0" true-label="1" v-model="row.emer_flag"></el-checkbox>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="req_dt" title="申请时间" width="120px"></vxe-table-column>
                  <vxe-table-column field="req_docno" title="申请医生" width="80px" align="center"></vxe-table-column>
                  <vxe-table-column field="input_dt" title="录入时间" width="120px"></vxe-table-column>
                  <vxe-table-column field="report_dt" title="报告时间" width="120px"></vxe-table-column>
                  <vxe-table-column field="reportuser" title="检验医生姓名" width="100px" align="center"></vxe-table-column>
                  <vxe-table-column field="item_num" title="项目数" width="120px" align="right" header-align="center"></vxe-table-column>
                  <vxe-table-column field="reportid" title="报告ID" width="120px" align="center"></vxe-table-column>
                  <vxe-table-column field="req_reason" title="检验目的" width="120px"></vxe-table-column>
              </vxe-table>
          </div>
          <div class="middle-right">
              <vxe-table :loading="loadingLeft" :data.sync="tableDataRight" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row ref="xTable2" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe class="vxeright">
                  <vxe-table-column field="cp_select" title="选择" width="60px" align="center">
                      <template v-slot="{ row }">
                          <el-checkbox true-label="1" false-label="0" v-model="row.cp_select" disabled></el-checkbox>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="sampleno" title="标本编号" width="100px" align="center"></vxe-table-column>
                  <vxe-table-column field="rpt_itemname" title="报告项目" width="100px"></vxe-table-column>
                  <vxe-table-column field="result_str" title="检验结果" width="80px" align="right" header-align="center"></vxe-table-column>
                  <vxe-table-column field="pat_name" title="病人姓名" width="80px" align="center"></vxe-table-column>
                  <vxe-table-column field="barcode" title="条码号" width="100px"></vxe-table-column>
                  <vxe-table-column field="pat_no" title="病人号" width="100px"></vxe-table-column>
                  <vxe-table-column field="specimen_name" title="标本类型" width="80px" align="center"></vxe-table-column>
                  <vxe-table-column field="reportid" title="报告ID" width="120px" align="center"></vxe-table-column>
              </vxe-table>
          </div>
      </div>

  </div>
</template>

<script type="text/javascript">
import XEUtils from "xe-utils";
XEUtils.mixin({
  // 自定义全局的格式化处理函数
  formatSelect(cellValue, list) {
      let item = list.find((item) => item.value == cellValue);
      return item ? item.label : "";
  },
});
export default {
  name: "LIS323",
  data() {
      return {
          loadingRight: false,
          loadingLeft: false,
          txtrptunit: "",
          sampleno: "",
          showbinded: "0",
          showalltargetitem: "0",
          sameorgreqno: "0",
          override: "0",
          samplenocopy: 0,
          patno: "",
          bindmdl: "",
          sampledatetime: "",
          inputMdlOptions: [],
          //表格数据
          tableDataRight: [],
          tableDataLeft: [],
          userid: "",
          mergeData: [],
          mergeindex: 0,
          errmsg: "",
          specimenname: "",
          tempindex: 0,
          selectrow: "",
          txtrpt_itemcode: "",
          sexOption: [
              { label: "男", value: "1" },
              { label: "女", value: "2" },
              { label: "未知", value: "3" },
          ],
          reload_right:0,
      };
  },
  created() {
      this.InitPageDate(); //初始化主列表
  },
  mounted() {},
  updated() {},
  methods: {
      inputmdlspecimen(list) {
          this.specimenname = list.specimen_name;
      },
      handlerrptitemchange(val) {
          this.txtrptunit = val;
          this.tableData = [];
      },
      InitPageDate() {
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS323",
                  method: "post",
                  data: {
                      action: "InitPageData",
                  },
              })
              .then((res) => {
                  this.inputMdlOptions = res.bindMdlData;
                  this.sampledatetime = res.now;
                  this.userid = res.userid;
              })
              .catch((e) => {});
      },
      Search() {
          if(!this.bindmdl){
              this.$alert("请先选择输入模板!", "提示");
              return;
          }
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS323",
                  method: "post",
                  data: {
                      action: "getrptlist",
                      rptunitid: this.txtrptunit,
                      sampledate: this.sampledatetime,
                      sampleno: this.sampleno,
                      patno: this.patno,
                      bindmdlid: this.bindmdl,
                      showbinded: this.showbinded,
                      showalltargetitem: this.showalltargetitem,
                      sameorgreqno: this.sameorgreqno,
                      overrideflag: this.override,
                  },
              })
              .then((res) => {
                  if (res) {
                      this.tableDataLeft = res.Data;
                      this.tableDataRight = [];
                      if(this.tableDataLeft && this.tableDataLeft.length > 0){
                          let row = this.tableDataLeft[0];
                          this.SearchBind(row);
                          this.tempindex = 0;
                          this.$refs.xTable1.setCurrentRow(row);
                      }
                  }
              })
              .catch((e) => {});
      },
      SearchBind(row) {
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS323",
                  method: "post",
                  data: {
                      action: "getbindrptlist",
                      rptunitid: row.rptunitid,
                      reportid: row.reportid,
                      sampledate: row.sampledate,
                      sampleno: row.sampleno,
                      patno: row.pat_no,
                      patname: row.pat_name,
                      original_reqno: row.original_reqno,
                      bindmdlid: this.bindmdl,
                      sameorgreqno: this.sameorgreqno,
                  },
              })
              .then((res) => {
                  this.tableDataRight = res.data;
                  this.mergeData = this.tableDataRight.filter((item) => item.cp_select == "1");
                  var temp = {}; //用于name判断重复
                  var result = []; //最后的新数组
                  this.mergeData.map(function (item, index) {
                      if (!temp[item.rpt_itemcode]) {
                          result.push(item);
                          temp[item.rpt_itemcode] = true;
                      }
                  });
                  this.mergeData = result;
                  this.mergeindex = 0;

                  if(this.reload_right==1){
                      this.reload_right=0;
                      this.canmerge();
                  }
              })
              .catch((e) => {});
      },
      canmerge() {
          this.reload_right==0;
          var row = this.$refs.xTable1.getCurrentRow();
          if (!row) {
              this.$alert("没有选中要合并的项目!", "提示");
              return;
          }
          if (this.mergeData.length == 0) {
              this.$alert("没有可以合并的项目!", "提示");
              return;
          }
          var row2 = this.mergeData[this.mergeindex];
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS323",
                  method: "post",
                  data: {
                      action: "canmerge",
                      rptunitid: this.txtrptunit,
                      reportid: row.reportid,
                      reportid2: row2.reportid,
                      sampledate: this.sampledatetime,
                      sampleno1: row.sampleno,
                      sampleno2: row2.sampleno,
                      pat_no: row.pat_no,
                      pat_no2: row2.pat_no,
                  },
              })
              .then((res) => {
                  if (res.errcode == "1") {
                      this.mergeaction();
                  } else if (res.errcode == "2") {
                      this.$confirm(res.msg, "提示", {
                          confirmButtonText: "确定",
                          cancelButtonText: "取消",
                          focusCancelButton: true,
                          closeOnClickModal: false,
                          type: "warning",
                      }).then(() => {
                          this.mergeaction();
                      });
                  } else if (res.errcode == "3"){
                      //重新刷新右边数据
                      this.reload_right=1;
                      this.SearchBind(row);
                  }
                  else {
                      this.$confirm(res.msg, "提示", {
                          confirmButtonText: "确定",
                          cancelButtonText: "取消",
                          focusCancelButton: true,
                          closeOnClickModal: false,
                          type: "warning",
                      });
                  }
              })
              .catch((e) => {});
      },
      mergeaction() {
          var row = this.$refs.xTable1.getCurrentRow();
          var row2 = this.mergeData[this.mergeindex];
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS323",
                  method: "post",
                  data: {
                      action: "mergeaction",
                      rptunitid: this.txtrptunit,
                      override: this.override,
                      reportid1: row.reportid,
                      reportid2: row2.reportid,
                      sampledate: this.sampledatetime,
                      rptitemcode: row.rpt_itemcode,
                      rptitemcode2: row2.rpt_itemcode,
                      bindmdlid: this.bindmdl,
                  },
              })
              .then((res) => {
                  if (res.errcode == "1") {
                      if (++this.mergeindex < this.mergeData.length) {
                          this.mergeaction();
                      } else {
                          if (this.errmsg) {
                              this.$confirm(this.errmsg, "提示", {
                                  confirmButtonText: "确定",
                                  cancelButtonText: "取消",
                                  focusCancelButton: true,
                                  closeOnClickModal: false,
                                  type: "warning",
                              });
                          } else {
                              this.$confirm("合并成功", "提示", {
                                  confirmButtonText: "确定",
                                  cancelButtonText: "取消",
                                  focusCancelButton: true,
                                  closeOnClickModal: false,
                                  type: "warning",
                              })
                                  .then(() => {
                                      this.Search();
                                  })
                                  .catch(() => {
                                      this.Search();
                                  });
                          }

                          this.mergeindex = 0;
                          this.errmsg = "";
                      }
                  } else {
                      this.errmsg += row2.rpt_itemname + "合并失败\r\n";
                      if (++this.mergeindex < this.mergeData.length) {
                          this.mergeaction();
                      } else {
                          this.$confirm(this.errmsg, "提示", {
                              confirmButtonText: "确定",
                              cancelButtonText: "取消",
                              focusCancelButton: true,
                              closeOnClickModal: false,
                              type: "warning",
                          });
                          this.mergeindex = 0;
                          this.errmsg = "";
                      }
                  }
              })
              .catch((e) => {});
      },

      currentChangeEvent({ row, rowIndex }) {
          this.SearchBind(row);
          this.tempindex = rowIndex;
      },
  },
};
</script>

<style scoped>
.pagetop {
  width: 100%;
  height: 120px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 125px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}

.middle-center {
  position: absolute;
  height: 100%;
  left: 0px;
  right: 505px;
  overflow: auto;
}

.middle-right {
  position: absolute;
  height: 100%;
  width: 500px;
  right: 0px;
}
.table1style tr td {
  padding: 0px 2px 0px 5px;
}
/deep/ .vxe-table .vxe-body--column.col-orange {
  background-color: #f8f8f8;
}
/deep/.el-checkbox__inner::after {
  border: 2px solid #000000 !important;
  box-sizing: content-box;
  content: "";
  border-left: 0 !important;
  border-top: 0 !important;
  height: 7px;
  left: 4px;
  position: absolute;
  top: 1px;
  transform: rotate(45deg) scaleY(0);
  width: 3px;
  transition: transform 0.15s ease-in 0.05s;
  transform-origin: center;
}
/deep/.el-checkbox__input.is-disabled + span.el-checkbox__label {
  color: #000000;
  cursor: not-allowed;
}
/deep/.el-checkbox__input.is-disabled .el-checkbox__inner,
.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner + .el-input.is-disabled .el-input__inner {
  background-color: #ffffff !important;
}
</style>